<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

class sa0Mail
{
    private $Host;
    private $Username;
    private $Password;
    private $Nickname;
    private $SMTPSecure;
    private $Port;

    public function __construct($data_)
    {
        $this->Host = $data_['host'];                // SMTP服务器
        $this->Username = $data_['username'];                     // SMTP 用户名  即邮箱的用户名
        $this->Password = $data_['password'];                 // SMTP 密码  部分邮箱是授权码(例如163邮箱)
        $this->Nickname = $data_['nickname'];       //发件人
        $this->SMTPSecure = $data_['SMTPSecure'];       //发件人
        $this->Port = $data_['port'];       //发件人
    }

    public function send($data_, $AltBody = '')
    {

        require __DIR__ . '/Exception.php';
        require __DIR__ . '/PHPMailer.php';
        require __DIR__ . '/SMTP.php';
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //服务器配置
            $mail->CharSet = "UTF-8";                     //设定邮件编码
            $mail->SMTPDebug = 0;                        // 调试模式输出
            $mail->isSMTP();                             // 使用SMTP
            $mail->Host = $this->Host;                // SMTP服务器
            $mail->SMTPAuth = true;                      // 允许 SMTP 认证
            $mail->Username = $this->Username;                // SMTP 用户名  即邮箱的用户名
            $mail->Password = $this->Password;             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
            $mail->SMTPSecure = $this->SMTPSecure;                    // 允许 TLS 或者ssl协议
            $mail->Port = $this->Port;                            // 服务器端口 25 或者465 具体要看邮箱服务器支持
            $mail->setFrom($this->Username, $this->Nickname);  //发件人
            $mail->addAddress($data_['addAddress']);  // 收件人
            $mail->addReplyTo($this->Username, $this->Nickname); //回复的时候回复给哪个邮箱 建议和发件人一致
            $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
            $mail->Subject = $data_['Subject'];
            $mail->Body = $data_['Body'];
            $mail->AltBody = $AltBody;
            $mail->send();
            return [true, null];
        } catch (Exception $e) {
            return [false, $mail->ErrorInfo];
        }
    }
}